﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
    //获取参数
    #region
    private Rigidbody2D rb;
    private Collider2D coll;
    private Animator anim;

    [Header("基础参数")]
    public float speed = 300f;
    public float jumpForce = 6f;
    float xVelocity;
    float yVelocity;

    [Header("玩家状态")]
    public bool isOnGround;
    public bool isJump;
    public bool isHurt;
   
    [Header("环境")]
    public LayerMask groundLayer;
    public LayerMask monster;

    [Header("其他组件")]
    public GameObject Monster;
   
    [Header("按键参数")]
    bool jumpPressed;
    bool isFacingLeft =false;
    bool usingMagnet  = false;
    float magentIsUsingUp =0f ;
    float magentIsUsingDown = 0f ;
    #endregion

    void Start()
    {
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<Collider2D>();
    }

    void Update()
    {
        PhysicsCheck();
        FacingDirection();
        Key();
        Jump();
    }

    void FixedUpdate()
    {
        PlayerMove();
    }
   
    //物理检测
    public void PhysicsCheck()
    {
        #region
        // Vector2 pos = transform.position;
        // Vector2 offset = new Vector2(0f, -0.49f);
        // RaycastHit2D downCheck = Physics2D.Raycast(pos + offset, Vector2.down, 0.15f, groundLayer);
        // Debug.DrawRay(pos + offset, Vector2.down, Color.red, 0.1f);
        #endregion
        RaycastHit2D downCheck = Raycast(new Vector2(0f, -0.49f), Vector2.down, 0.2f, groundLayer);
        RaycastHit2D destroyMonster = Raycast(new Vector2(0f, -0.49f), Vector2.down, 0.2f, monster);

        if (destroyMonster)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            Destroy(Monster);
        }
        isOnGround = downCheck;
        //anim.SetBool("isOnGround",isOnGround);
    }
    
    //触碰死亡线
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Dieline")
        {
            Invoke("Restart", 1f);
        }
    }
    
    //角色移动
    public void PlayerMove()
    {
        xVelocity = Input.GetAxisRaw("Horizontal");
        if (!isHurt)
            rb.velocity = new Vector2(xVelocity * speed * Time.deltaTime, rb.velocity.y);
    }
  
    //跳跃
    private void Jump()
    {
        if (jumpPressed && !isHurt && isOnGround)
        {
            isJump = true;
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    //角色朝向
    public void FacingDirection()
    {
        if (xVelocity < 0) 
        {
            transform.localScale = new Vector2(-1f, 1f);
            isFacingLeft = true;
        }
        if (xVelocity > 0) 
        {
            transform.localScale = new Vector2(1f, 1f);
            isFacingLeft = false;
        }
    }
   
    //重新开始（去世）
    public void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        Debug.Log("RestartGame");
        Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), LayerMask.NameToLayer("Player"), true);
        isHurt = false;
    
    }


    //按键控制
    public void Key()
    {
        if (Input.GetKey(KeyCode.R))
        {
            Restart();
        }

        //UseMagnet();
        jumpPressed = Input.GetKeyDown(KeyCode.Space);
    }
   
    //射线检测
    RaycastHit2D Raycast(Vector2 offset, Vector2 rayDirection, float length, LayerMask layer)
    {
        Vector2 pos = transform.position;

        RaycastHit2D hit = Physics2D.Raycast(pos + offset, rayDirection, length, layer);

        Color color = hit ? Color.green : Color.red;

        Debug.DrawRay(pos + offset, rayDirection * length, color);

        return hit;
    }
  
    //受伤反映
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Monster")
        {

            rb.velocity = new Vector2(rb.velocity.x,5f);
            #region
            // if (transform.position.x < collision.gameObject.transform.position.x)
            // {
            //     rb.velocity = new Vector2(-10f, rb.velocity.y);
            //     isHurt = true;
            // }
            // else if (transform.position.x > collision.gameObject.transform.position.x)
            // {
            //     rb.velocity = new Vector2(10f, rb.velocity.y);
            //     isHurt = true;
            // }
            //
            #endregion
            this.coll.enabled = false;
            //Invoke("Restart", 1f);
        }
    }
}